package controllers

import (
	"fmt"
	"encoding/json"
	"github.com/beego/beego/v2/server/web"
	. "neutron_probe/requests/yAnalyser"
	. "neutron_probe/requests/common"
	. "neutron_probe/utils"
	yAnalyserDao "neutron_probe/daos/yAnalyser"
)
	
// Operations about YAnalyser
type YAnalyserController struct {
	web.Controller
}


// @Title 创建 YAnalyser
// @Description 创建 中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 body is empty
// @router /neutron_probe/yanalyser/createObj [post]
func (this *YAnalyserController) CreateObj() {
		var err error
		var request CreateObj
		if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
			  
			request.Token = Uuid()
			
			yAnalyser := request.Convert2YAnalyser()
			if _ ,err := yAnalyserDao.CreateObj(&yAnalyser);err == nil {
				this.Data["json"] = map[string]interface{}{"code":0}
			} else {
				Logger.Warn(fmt.Sprintf("CreateObj %s",err.Error()))
				this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
			}
		} else {
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
		this.ServeJSON()
}
// @Title getObjById
// @Description 根据Id获取中子仪数据
// @Param	body		body 	string	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 body is empty
// @router /getObjById [post]
func (this *YAnalyserController) GetObjById() {
	var err error
	var request GetById
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if obj,err := yAnalyserDao.GetObjById(request.Id);err == nil {
			this.Data["json"] = map[string]interface{}{"value": obj,"code":0}
		} else {
			Logger.Warn(fmt.Sprintf("GetObjById %s %s",StringPtrAsTxt(request.Id),err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
}
// @Title updateObj
// @Description 更新中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 body is empty
// @router /updateObj [post]
func (this *YAnalyserController) UpdateObj() {
	var err error
	var request UpdateObj
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		yAnalyser := request.Convert2YAnalyser()
		if _,err := yAnalyserDao.UpdateObj(&yAnalyser);err == nil {
			this.Data["json"] = map[string]interface{}{"code":0}
		} else {
			Logger.Warn(fmt.Sprintf("UpdateObj %s",err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
}

// @Title listObj
// @Description 列出 中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 body is empty
// @router /listObj [post]
func (this *YAnalyserController) ListObj() {
	if list,err := yAnalyserDao.ListObj();err == nil {
		this.Data["json"] = map[string]interface{}{"code":0,"list":list}
	} else {
		Logger.Warn(fmt.Sprintf("ListObj %s",err.Error()))
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
}

// @Title Count4Page
// @Description 分页方式列出中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 
// @router /pageObj [post]
func (this *YAnalyserController) Count4Page() {
	var err error
	var request SearchInfo
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if count,err := yAnalyserDao.Count4Page(request.GetConditions());err == nil {
			this.Data["json"] = map[string]interface{}{"code":0,"count":count}
		} else {
			Logger.Warn(fmt.Sprintf("Count4Page %s",err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
	return
}

// @Title PageObj
// @Description 分页方式列出中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 
// @router /pageObj [post]
func (this *YAnalyserController) PageObj() {
	var err error
	var request PageObj
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if list,err := yAnalyserDao.PageObj(request.SearchInfo.GetConditions(),request.GetOrder(),request.PageInfo.Page,request.PageInfo.PageSize);err == nil {
			this.Data["json"] = map[string]interface{}{"code":0,"list":list}
		} else {
			Logger.Warn(fmt.Sprintf("PageObj %s",err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
	return
}

// @Title DeleteObj
// @Description 删除中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} 
// @Failure 403 
// @router /deleteObj [post]
func (this *YAnalyserController) DeleteObj() {
	var err error
	var request GetById
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if count,err := yAnalyserDao.DeleteObj(request.Id);err == nil {
			this.Data["json"] = map[string]interface{}{"code":0,"count":count}
		} else {
			Logger.Warn(fmt.Sprintf("DeleteObj %s",err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
	return
}

// @Title deleteObjs 
// @Description create 中子仪数据
// @Param	body		body 	models.YAnalyser	true		"body for yAnalyser content"
// @Success 200 {int} models.YAnalyser.Id
// @Failure 403 body is empty
// @router /deleteObjs [delete]
func (this *YAnalyserController) DeleteObjs() {
	var err error
	var request IdsReq
	count := 0
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		for _,id := range request.Ids {
			if _,err = yAnalyserDao.DeleteObj(id);err != nil {
				Logger.Warn(fmt.Sprintf("DeleteObjs %s",err.Error()))
				this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
				this.ServeJSON()
				return
			} 
			count++
		}
		this.Data["json"] = map[string]interface{}{"code":0,"count":count}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(),"code":-1}
	}
	this.ServeJSON()
	return
}

func init() {
    web.Router("/neutron_probe/yanalyser/listObj", &YAnalyserController{},"get,post:ListObj")
    web.Router("/neutron_probe/yanalyser/deleteObjs", &YAnalyserController{},"post:DeleteObjs")
    web.Router("/neutron_probe/yanalyser/deleteObj", &YAnalyserController{},"post:DeleteObj")
    web.Router("/neutron_probe/yanalyser/pageObj", &YAnalyserController{},"post:PageObj")
    web.Router("/neutron_probe/yanalyser/count4Page", &YAnalyserController{},"post:Count4Page")
    web.Router("/neutron_probe/yanalyser/updateObj", &YAnalyserController{},"post:UpdateObj")
    web.Router("/neutron_probe/yanalyser/getObjById", &YAnalyserController{},"post:GetObjById")
    web.Router("/neutron_probe/yanalyser/createObj", &YAnalyserController{},"post:CreateObj")
}
